package com.bbs.utils;

import com.bbs.pojo.UserPrincipal;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtil {
    /**
     * 返回当前用户
     */
    public static UserPrincipal getCurrentUser(){
        UserPrincipal userPrincipal = null;
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if(authentication != null){
            Object principal = authentication.getPrincipal();
            if(principal instanceof UserPrincipal){
                userPrincipal = (UserPrincipal) principal;
            }
        }

        return userPrincipal;
    }
}
